package com.adong.juc;

import java.util.concurrent.*;

/**
 * 线程池简单创建
 */
public class MyThreadPoolDemo {
    public static void main(String[] args) {
        ExecutorService executorService = new ThreadPoolExecutor(
                2,
                5,
                3L,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );

        for (int i = 1; i <= 3; i++) {
            executorService.execute(() -> {
                System.out.println(Thread.currentThread().getName() + "\t号业务员处理业务");
            });

        }

    }

    private static void executorsTest() {
        ExecutorService threadPool01 = Executors.newFixedThreadPool(3);
        ExecutorService threadPool02 = Executors.newCachedThreadPool();
        ExecutorService threadPool03 = Executors.newSingleThreadExecutor();

        try {
            for (int i = 1; i <= 3; i++) {
                threadPool01.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + "\t号业务员处理业务");
                });

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool01.shutdown(); // 线程池任务完成,关闭线程池
        }
    }
}
